import { message } from 'antd/lib/index';
import * as api from '../api/invite';
import checkStatus from '@/utils/status';

export default {
  namespace: 'Invite',

  state: {
    data: {
      data: [],
      pagination: {},
    },
  },
  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(api.getList, payload);
      yield checkStatus(response);
      yield put({
        type: 'get',
        payload: response.msg,
      });
    },
    * detail({ payload }, { call, put }) {
      const response = yield call(api.detail, payload);
      yield checkStatus(response);

      yield put({
        type: 'Detail',
        payload: response.msg,
      });
    },
    * confirm({ payload }, { call }) {
      const response = yield call(api.confirm, payload);
      yield checkStatus(response);
      if (response.code === 200) {
        message.success('放款成功');
      } else {
        message.error(response.errMsg);
      }
    },
  },

  reducers: {
    get(state, action) {
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        ...state,
        data: action.payload,
      };
    },
    Detail(state, action) {
      return {
        ...state,
        details: action.payload,
      };
    },
  },
};
